<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPermission(1,'0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-card>
        <!-- row-key 是使用属性表格时必须的设定
        这个属性, 并不是指向某个变量 -->
        <!-- 只是告诉 table 在遍历行的时候, 使用当前行的那个数据作为 key 值
        传入一个字符串 即可 -->
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column align="center" label="标识" prop="code" />
          <el-table-column align="center" label="描述" prop="description" />
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="addPermission(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="editPermission(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>
      </el-card>

      <el-dialog :visible="showDialog" :title="formData.id?'编辑权限':'新增权限'" @close="btnCancel">
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <!-- 默认情况开关绑定的值是 true / false
            如果有其他需求可以用 active-value / inactive-value 自定义开关状态对应值 -->
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>

    </div>
  </div>
</template>

<script>
// 引入树形数据的转换函数
import { transListToTreeData } from '@/utils'
import { getPermissionList, addPermission, getPermissionDetail, updatePermission, delPermission } from '@/api/permission'
export default {
  data() {
    return {
      // 是否显示弹窗
      showDialog: false,
      // 全部权限列表
      list: [],
      // 弹出窗的表单数据对象
      formData: {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionList()
      console.log(res)
      this.list = transListToTreeData(res, '0')
      console.log(this.list)
    },
    addPermission(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    async editPermission(id) {
      // 1.回显数据
      this.formData = await getPermissionDetail(id)
      // 2.弹出弹窗
      this.showDialog = true
    },
    async delPermission(id) {
    //  二次确认
      // 确认的方法是在 饿了么组件库 MessageBox 里面的确认消息功能
      await this.$confirm('是否确认删除权限')
      //  请求
      await delPermission(id)
      //  更新数据
      this.getPermissionList()
      this.$message.success('删除成功')
    },
    async btnOK() {
      console.log(this.formData)
      // 校验
      await this.$refs.perForm.validate()
      // ajax请求
      // 需要区分新增和编辑情况
      if (this.formData.id) {
        // 编辑
        await updatePermission(this.formData)
      } else {
        // 添加
        await addPermission(this.formData)
      }
      // 提示成功
      this.$message.success('操作成功')
      // 重新加载数据
      this.getPermissionList()
      // 关闭弹窗
      this.showDialog = false
    },
    btnCancel() {
      // 1. 清空表单
      this.formData = {
        enVisible: '1',
        name: '',
        code: '',
        description: '',
        type: '',
        pid: ''
      }
      // 2.清空校验数据
      this.$refs.perForm.resetFields()
      // 3.关闭弹窗
      this.showDialog = false
    }
  }
}
</script>

<style>

</style>
